package shelly;

import shelly.common.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Shelly
 * @create: 2023-01-13 21:28:20
 * @describe:
 */
public class Util {
    /**
     * 两数交换
     * @param arr
     * @param i
     * @param j
     */
    public static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void swapInteger(Integer[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    ///////////////////////单链表 begin//////////////////////////////
    /**
     * for test:获取指定长度内的随机单链表：取值范围整型 0-value
     * @param len
     * @param value
     * @return
     */
    public static Node generateRandomLinkedList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) {
            return null;
        }
        size--;
        Node head = new Node((int) (Math.random() * (value + 1)));
        Node pre = head;
        while (size != 0) {
            Node cur = new Node((int) (Math.random() * (value + 1)));
            pre.next = cur;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * for test：获取node转list
     * @param head
     * @return
     */
    public static List<Integer> getLinkedListOriginOrder(Node head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    /**
     * 打印链表
     * @param list
     */
    public static void printLinkedList(List<Integer> list) {
        for (int i = 0;i<list.size();i++){
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
    }

    /**
     * for test:比较List和Node的各个节点的值
     * @param origin
     * @param head
     * @return
     */
    public static boolean checkLinkedListAndNode(List<Integer> origin, Node head) {
        for (int i =0; i < origin.size(); i++) {
            if (!origin.get(i).equals(head.value)) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
    /**
     * for test:比较List和Node的各个节点的值 反转
     * @param origin
     * @param head
     * @return
     */
    public static boolean checkLinkedListReverse(List<Integer> origin, Node head) {
        for (int i = origin.size()-1; i >=0 ; i--) {
            if (!origin.get(i).equals(head.value)) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
    ///////////////////////单链表 end//////////////////////////////
    ///////////////////////双链表//////////////////////////////////



}
